﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace Barbar.MapReport.Common.Drawing {
  /// <summary>
  /// Popisuje praci se serverovou cachi
  /// </summary>
  [XmlType(Namespace = Constants.XmlNamespace)]
  [Serializable]
  public class DataLocationServer : DataLocation {
    private TimeSpan m_AbsoluteExpiration = new TimeSpan(0, 1, 0);
    private TimeSpan m_SlidingExpiration = TimeSpan.Zero;
    private bool m_UseAbsoluteExpiration = true;
    private bool m_UseSlidingExpiration;
    private string m_Id;

    /// <summary>
    /// Vychozi hodnota serverove cache je jedna minuta po vytvoreni objektu
    /// </summary>
    public DataLocationServer() {
    }

    private void EnsureId() {
      if (String.IsNullOrEmpty(m_Id))
        m_Id = Guid.NewGuid().ToString("N");
    }

    /// <summary>
    /// Identifikator objektu v serverove cachi - jen pro serializaci, nepouzivat
    /// </summary>
    public string Id {
      get {
        EnsureId();
        return m_Id;
      }
      set {
        m_Id = value;
        EnsureId();
      }
    }

    /// <summary>
    /// Pouzit absolutni expiraci cache
    /// </summary>
    public bool UseAbsoluteExpiration {
      get { return m_UseAbsoluteExpiration; }
      set { m_UseAbsoluteExpiration = value; }
    }

    /// <summary>
    /// Pouzit expiraci cache s prodlouzenim (kazdy pristup zvysi delku zivota objektu v cachi)
    /// </summary>
    public bool UseSlidingExpiration {
      get { return m_UseSlidingExpiration; }
      set { m_UseSlidingExpiration = value; }
    }

    /// <summary>
    /// Pocet ticku (v milisekundach) po kterou bude objekt v cachi
    /// </summary>
    public long AbsoluteExpirationTicks {
      get { return m_AbsoluteExpiration.Ticks; }
      set { m_AbsoluteExpiration = new TimeSpan(value); }
    }

    /// <summary>
    /// Doba po kterou bude objekt v cachi
    /// </summary>
    [XmlIgnore()]
    public TimeSpan AbsoluteExpiration {
      get { return m_AbsoluteExpiration; }
      set { m_AbsoluteExpiration = value; }
    }

    /// <summary>
    /// Doba prodlouzeni delky zivota objektu v cachi (v milisekundach)
    /// </summary>
    public long SlidingExpirationTicks {
      get { return m_SlidingExpiration.Ticks; }
      set { m_SlidingExpiration = new TimeSpan(value); }
    }

    /// <summary>
    /// Doba prodlouzeni delky zivota objektu v cachi
    /// </summary>
    [XmlIgnore()]
    public TimeSpan SlidingExpiration {
      get { return m_SlidingExpiration; }
      set { m_SlidingExpiration = value; }
    }
  }
}
